﻿using Antlr.Runtime.Tree;
using Hr.BLL;
using Hr.Model;
using HR.HrHelper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace HR.Areas.letao.Controllers.Dashboard
{
    public class DashboardMangeController : ApiController
    {
        private DashboardService DashboardService = new DashboardService();
        int code = 0;
        string msg = "success";

        /// <summary>
        /// 控制面板获取所有相关信息
        /// </summary>
        /// <param name="pk_User"></param>
        /// <returns></returns>
        [HttpGet]
        public JObject GetDashboardInfo(int pk_User)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",DashboardService.GetDashboardInfo(pk_User)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        #region 便签操作
        /// <summary>
        /// 便签，模糊查询，分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="Term"></param>
        /// <returns></returns>
        [HttpGet]
        public JObject GetMemoList(int page,int limit,string Term,int pk_User)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",DashboardService.GetMemoList(pk_User,page,limit,Term)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 添加、修改便签
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateMemo()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            Memo_List M = JsonHelper.DeserializeJsonToObject<Memo_List>(RequestForm["Memo"]);
            string Type = RequestForm["Type"];
            int pk_User = int.Parse(RequestForm["pk_User"].ToString());
            msg = "成功";
            if (!DashboardService.UpdateMemo(M, Type, pk_User))
            {
                code = 1;
                msg = "失败";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 删除便签
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject DelMemo()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            int pk_Memo = int.Parse(RequestForm["pk_Memo"].ToString());
            Memo_List M = DashboardService.GetMemoInfo(pk_Memo);
            msg = "删除成功";
            if (M != null)
            {
                if (!DashboardService.DelMemo(M))
                {
                    code = 1;
                    msg = "删除失败";
                }
            }
            else
            {
                code = 1;
                msg = "便签不存在";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion

        #region 个人设置操作

        /// <summary>
        /// 根据ID获取用户信息
        /// </summary>
        /// <param name="pk_User"></param>
        /// <returns></returns>
        [HttpGet]
        public JObject GetUserListInfo(int pk_User)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",DashboardService.GetUserListInfo(pk_User)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 修改用户头像
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateImg()
        {
            //int pk_User,string URL
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            int pk_User = int.Parse(RequestForm["pk_User"].ToString());
            string URL = RequestForm["URL"];
            User_List UL = DashboardService.GetUserListInfo(pk_User);
            msg = "成功";
            if (UL != null)
            {
                if (!DashboardService.UpdateImg(pk_User, URL))
                {
                    code = 1;
                    msg = "失败";
                }
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",FileHelper.GetImgInfo(URL) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));

        }


        /// <summary>
        /// 个人中心修改用户信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateUserInfo()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            //获取用户信息表单
            User_List UL = JsonHelper.DeserializeJsonToObject<User_List>(RequestForm["UserInfo"]);
            int pk_User = int.Parse(RequestForm["pk_User"].ToString());

            msg = "保存成功";
            User_List U = DashboardService.GetUserListInfo(pk_User);
            if (U != null)
            {
                if (ModelHelper.ReflectModel(UL, U))
                {
                    msg = "无任何更新";
                }
                else
                {
                    //修改更新
                    if (!DashboardService.UpdateUserInfo(UL, pk_User))
                    {
                        code = 1;
                        msg = "保存失败";
                    }
                }
            }
            else
            {
                code = 1;
                msg = "用户不存在，请重新登陆";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",U }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        #endregion
    }
}
